/*
 * @Author: mark
 * @Date: 2023-05-11 08:59:27
 * @LastEditors: mark
 * @LastEditTime: 2023-07-11 09:47:17
 * @Description: file content
 */
import axios from 'axios';
import { ElMessage } from 'element-plus';
import { formData, updateData, exports, formDatas } from './includes'; // 引入状态码文件
import url from './common.ts';
const Logout = url.Logout;
// import { store } from '@/store/index';
const instance = axios.create({
  timeout: 10000, // 设置超时时间10s
  baseURL: '/api',
});
let msg = false;
// 文档中的统一设置post请求头。下面会说到post请求的几种'Content-Type'
// instance.defaults.headers['Content-Type'] = 'application/json;charset=utf-8';
instance.defaults.headers.post['Content-Type'] = 'application/json';

/** 新增请求拦截器 * */
instance.interceptors.request.use(
  (config: any) => {
    // 配置请求头
    const token = localStorage.getItem('token') || '';
    const flagState = formData(config.url);
    const updateState = updateData(config.url);
    const exportsState = exports(config.url);
    const formDataState = formDatas(config.url);
    config.headers.Authorization = `Bearer ${token}`;
    if (updateState) {
      msg = true;
    } else if (flagState) {
      if (formDataState) {
        config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
      }
      msg = false;
    } else if (exportsState) {
      config.headers['Content-Type'] = 'application/octet-stream';
      config.responseType = 'blob';
    } else if (formDataState) {
      //formdata传参方式
      msg = true;
      config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
    } else {
      msg = false;
    }
    return config;
  },
  (error) => Promise.reject(error)
);

/** 新增响应拦截器  * */
instance.interceptors.response.use(
  (response: any) => {
    if (!response.data.hasOwnProperty('code')) {
      //文件流
    } else if (response.data.code == 200) {
      if (msg) {
        ElMessage({ message: response.data.msg, type: 'success' });
      }
    } else if (response.data.code == 401) {
      ElMessage({ message: response.data.msg, type: 'error' });
      setTimeout(() => {
        Logout();
      }, 0);
    } else if (response.data.code == 500) {
      ElMessage({ message: response.data.msg, type: 'error' });
    } else if (response.data.code == 701) {
      ElMessage({ message: response.data.msg, type: 'warning' });
    } else {
      ElMessage({ message: response.data.msg, type: 'error' });
    }
    return Promise.resolve(response.data);
  },
  (error) => {
    const { response } = error;
    if (response) {
      // 请求已发出，但是不在2xx的范围
      ElMessage({ message: response.data.msg, type: 'error' });
      return Promise.reject(response.data);
    } else {
      ElMessage({ message: '请求出错，请查看接口数据', type: 'error' });
      return Promise.reject('请求出错，请查看接口数据');
    }
  }
);

/* 统一封装get请求 */
export const get = (url = '', params = {}, config = {}) =>
  new Promise((resolve, reject) => {
    instance({
      method: 'get',
      url,
      params,
      ...config,
    })
      .then((response) => {
        resolve(response);
      })
      .catch((error) => {
        reject(error);
      });
  });

/* 统一封装post请求  */
export const post = (url = '', data:any = '', config:any = {}) =>
  new Promise((resolve, reject) => {
    instance({
      method: 'post',
      url,
      data,
      ...config,
    })
      .then((response) => {
        resolve(response);
      })
      .catch((error) => {
        reject(error);
      });
  });
/* 统一封装put请求  */
export const put = (url = '', data = {}, config = {}) =>
  new Promise((resolve, reject) => {
    instance({
      method: 'put',
      url,
      data,
      ...config,
    })
      .then((response) => {
        resolve(response);
      })
      .catch((error) => {
        reject(error);
      });
  });
/* 统一封装delete请求  */
export const deletes = (url = '', data = '', config = {}) =>
  new Promise((resolve, reject) => {
    instance({
      method: 'delete',
      url,
      data,
      ...config,
    })
      .then((response) => {
        resolve(response);
      })
      .catch((error) => {
        reject(error);
      });
  });
